package com.shycloud.mido.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.member.api.vo.AtvMainOfflineVO;
import java.util.ArrayList;
import javax.validation.constraints.NotBlank;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.api.entity.AtvMainOffline;
import com.shycloud.mido.member.mapper.AtvMainOfflineMapper;
import com.shycloud.mido.member.service.AtvMainOfflineService;

@Service
public class AtvMainOfflineServiceImpl extends ServiceImpl<AtvMainOfflineMapper, AtvMainOffline> implements AtvMainOfflineService {

	@Override
	public List<AtvMainOfflineVO> getAtvMainList(String memberId) {

		QueryWrapper<AtvMainOffline> qw = new QueryWrapper();

		if (StringUtils.isNotBlank(memberId)) {
			qw.eq("from_member_id", memberId);
		}

		List<AtvMainOffline> atvMainOfflines = baseMapper.selectList(qw.orderByDesc("status"));

		List<AtvMainOfflineVO> result = new ArrayList<>();

		for (AtvMainOffline atvMainOffline : atvMainOfflines) {
			AtvMainOfflineVO atvMainOfflineVO = new AtvMainOfflineVO();
			BeanUtils.copyProperties(atvMainOffline, atvMainOfflineVO);
			//循环处理 URL
			String atvPicUrls = atvMainOffline.getAtvPicUrls();
			if (StringUtils.isNotBlank(atvPicUrls)) {
				String[] atvPicUrlArr = atvPicUrls.split(",");
				atvMainOfflineVO.setAtvPicUrlArr(atvPicUrlArr);
			}
			String orgPicUrls = atvMainOffline.getOrgPicUrls();
			if (StringUtils.isNotBlank(orgPicUrls)) {
				String[] orgPicUrlArr = orgPicUrls.split(",");
				atvMainOfflineVO.setOrgPicUrlArr(orgPicUrlArr);
			}
			result.add(atvMainOfflineVO);
		}
		return result;
	}

	@Override
	public IPage getAtvMainListPage(Page page, String atvSubject, String status) {

		IPage<AtvMainOfflineVO> atvMainOfflines = baseMapper.getAtvMainListPage(page, atvSubject, status);

		if (atvMainOfflines != null) {

			List<AtvMainOfflineVO> result = new ArrayList<>();

			for (AtvMainOffline atvMainOffline : atvMainOfflines.getRecords()) {
				AtvMainOfflineVO atvMainOfflineVO = new AtvMainOfflineVO();
				BeanUtils.copyProperties(atvMainOffline, atvMainOfflineVO);
				//循环处理 URL
				String atvPicUrls = atvMainOffline.getAtvPicUrls();
				if (StringUtils.isNotBlank(atvPicUrls)) {
					String[] atvPicUrlArr = atvPicUrls.split(",");
					atvMainOfflineVO.setAtvPicUrlArr(atvPicUrlArr);
				}
				String orgPicUrls = atvMainOffline.getOrgPicUrls();
				if (StringUtils.isNotBlank(orgPicUrls)) {
					String[] orgPicUrlArr = orgPicUrls.split(",");
					atvMainOfflineVO.setOrgPicUrlArr(orgPicUrlArr);
				}
				result.add(atvMainOfflineVO);
			}
			atvMainOfflines.setRecords(result);
		}

		return atvMainOfflines;

	}
}
